/**
 * 截取url后缀的对应参数值
 */
var getUrlParam = function(key){
    var str = location.href.split('?')[1];
    if(str=="" || str==null)
        return null;
    var params = str.split('&');
    for(var i = 0; i < params.length ; i++){
        var param = params[i].split('=');
        if(param[0]==key){
            return param[1];
        }
    }
}
/**
 * 获取接口调用的url
 */
var getAjaxUrl = function(path,data){
    //测试
    //var url = '/viptail/api/forward/dog2Test?path='+path;
    //正式
    var url = '/viptail/api/forward/dog2?path='+path;
    if(!data || data==null || data==''){
        return url;
    }else{
        return url+'&params='+JSON.stringify(data);
    }
}
/**
 * 获取屏幕宽度
 * @returns {Number}
 */
function getPageWidth(){
    var pageWidth = top.window.innerWidth;
    if(typeof pageWidth != "number"){
        var doc = top.document;
        pageWidth = doc.documentElement.clientWidth ? doc.documentElement.clientWidth:doc.body.clientWidth;
    }
    return pageWidth;
}
/**
 * 加载中 开启动画
 */
var toolImgDir = 'img/dist/';
var loadingTimer = null;
var order = 1;
var loadingAnimation = function(loadImgs){
    loadImgs.hide();
    switch (order++){
        case 1: $(loadImgs[0]).show();break;
        case 2: $(loadImgs[1]).show();break;
        case 3: $(loadImgs[2]).show();break;
        case 4: $(loadImgs[3]).show();break;
        case 5: $(loadImgs[4]).show();break;
    }
    if(order > 5){
        order = 1;
    }
}
var loading = function(){
    var load = $('#loading'),loadImgs;
    if(load.length==0 || load==undefined || load==null){
        load = $('<div>').attr('id','loading');
        $('<img>').attr('src',toolImgDir+'load_animation_one@2x.png').appendTo(load);
        $('<img>').attr('src',toolImgDir+'load_animation_two@2x.png').appendTo(load);
        $('<img>').attr('src',toolImgDir+'load_animation_three@2x.png').appendTo(load);
        $('<img>').attr('src',toolImgDir+'load_animation_four@2x.png').appendTo(load);
        $('<img>').attr('src',toolImgDir+'load_animation_five@2x.png').appendTo(load);
        //覆盖整个页面的加载动画
        var pageWidth = getPageWidth();
        load.css({left: pageWidth/2-35}).appendTo($('body'));
    }
    loadImgs = load.children('img');
    load.show();
    loadingTimer = setInterval(function(){
        loadingAnimation(loadImgs);
    },100);
}
/**
 * 加载结束 关闭动画
 */
var loaded = function(){
    clearInterval(loadingTimer);
    $('#loading').fadeOut(200);
}
/**
 * 本地存储(若不支持HTML本地存储 则用cookie)
 * @param data
 */
var setLocalStorage = function(key,value){
    if(value instanceof Object){
        value = JSON.stringify(value);
    }
    if (window.localStorage) {
        localStorage.setItem(key, code(value));
    } else {
        Cookie.write(key, code(value));
    }
}
var getLocalStorage = function(key){
    return uncode(window.localStorage ? localStorage.getItem(key): Cookie.read(key));
}
var delLocalStorage = function(key){
    if (window.localStorage) {
        localStorage.removeItem(key);
    }
}
/**
 * 加密方法
 * @param value
 * @returns {*}
 */
var code = function(value){
    if(value=="" || value==null){
        return "";
    }
    value = value.toString();//转换成字符串
    $.base64.utf8encode = true;
    if(value instanceof Object){
        value.jsonData = JSON.stringify(value.jsonData);
        value = JSON.stringify(value);
    }
    value = $.base64.encode(value);
    var middle = value.substring(1,3).split("").reverse().join("");
    var arr0 = value.charAt(0);
    var arr1 = value.substring(3,value.length);
    return $.base64.encode(arr0.concat(middle.concat(arr1)));
}
/**
 * 解密参数
 * @param value
 * @returns {*|String}
 */
var uncode = function(value){
    $.base64.utf8decode = true;
    value = $.base64.decode(value);
    var label = value.substring(1,3).split("").reverse().join("");
	var arr0 = value.charAt(0);
	var arr1 = value.substring(3,value.length);
    return $.base64.decode(arr0.concat(label.concat(arr1)));
}
/**
 * JSON数据的参数分别加密
 * @param data
 * @returns {{}}
 */
var paramCoding = function(data){
    var codedData = {};
    $.each(data,function(key,value){
        if(key=="?"){
            //不需加密的参数 图片等
            codedData[key] = value;
        }else{
            codedData[key] = code(value);
        }
    });
    return codedData;
}
/**
 * 提示框
 * @param message
 */
var showMessageAlert = function(message,width){
    var messageAlertDiv = $("#messageAlert");
    if(messageAlertDiv.length==0 || messageAlertDiv==undefined || messageAlertDiv==null){
        messageAlertDiv = $("<div>").attr("id","messageAlert")
            .addClass("message_alert");
        $("<p>").html(message)
            .css({width: ((width?width:message.length)+2)+"em"})
            .appendTo(messageAlertDiv);
        $("body").append(messageAlertDiv);
    }else{
        messageAlertDiv.find("p").html(message)
            .css({width:  ((width?width:message.length)+2)+"em"});
        messageAlertDiv.show();
    }
    setTimeout(function(){
        messageAlertDiv.hide();
    },2000);
}
/**
 * 禁止\恢复滚动
 */
var scrolling = true;
var cover = $('.cover');//遮罩
var stopScrolling = function(){
    scrolling = false;
    cover.on('touchmove',function(e){
        if(!scrolling){
            e.preventDefault();
            e.returnValue = false;
            e.stopPropagation();
            return false;
        }
    })
}
var restartScrolling = function(){
    scrolling = true;
}
/**
 * 是否为微信
 * @returns {boolean}
 */
function isWechat() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    } else {
        return false;
    }
}
/**
 * 输入框获得焦点 底部banner相对定位
 */
function unfixedBottom(input){
    $('.submit').css({
        position: 'absolute'
    })
}
/**
 * 底部回到fixed定位
 */
function fixedBottom(input){
    $('.submit').css({
        position: 'fixed'
    })
}
